/*
  学习目标：ref获取组件实例对象
*/

import React, { Component } from 'react';

export default class App extends Component {
  // 1. 创建ref

  childRef = React.createRef();

  render() {
    return (
      <div>
        <h1>我是App</h1>
        <Child
          // 2. 绑定ref
          ref={this.childRef}
        ></Child>
        <button
          onClick={() => {
            // 3. 通过ref.current获取组件实例对象
            this.childRef.current.handleAlert();
            console.log(
              'this.childRef.current.state.count  ----->  ',
              this.childRef.current.state.count
            );
          }}
        >
          点我
        </button>
      </div>
    );
  }
}

class Child extends React.Component {
  state = {
    count: 100,
  };

  handleAlert() {
    alert(123);
  }

  render() {
    return <div>Child</div>;
  }
}
